package cfl.activemq.com.topics;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;

import cfl.activemq.com.config.ActiveMQConfig;

/**
 * 消费者
 * @author chenfenli
 *
 */
public class TopicsConsumer {

	public static void main(String[] args) {
		
		ConnectionFactory connectionFactory;
		Connection connection = null;
		try {
			// 1.创建链接工厂
			connectionFactory = new ActiveMQConnectionFactory(ActiveMQConfig.UASENAME, ActiveMQConfig.PASSWORD, ActiveMQConfig.BROKEURL);
			
			// 2.创建链接对象
			connection = connectionFactory.createConnection();
			
			// 3.启动链接
			connection.start();
			
			// 4.创建Session，false：不开启事务
			Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			
			// 5.创建订阅 名称必须和生产者一致
			Destination destination = session.createTopic("Topics测试");
			
			// 6.创建消费者
			MessageConsumer messageConsumer = session.createConsumer(destination);
			
			// 7.监听消息
			messageConsumer.setMessageListener(new TopicsListener());
			
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
}
